package benny_driving_ser.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import atg.taglib.json.util.JSONArray;
import atg.taglib.json.util.JSONException;
import atg.taglib.json.util.JSONObject;
import benny_driving_ser.ObjectBean.DriHistoryOrderBean;
import benny_driving_ser.ObjectBean.UserHistoryOrderBean;
import benny_driving_ser.ObjectBean.UserOrderBean;
import benny_driving_ser.dao.OrderDao;
import benny_driving_ser.dao.impl.OrderDaoImpl;
import benny_driving_ser.entity.SessionAttributeDriId;
import benny_driving_ser.entity.SessionAttributeUserId;
import benny_driving_ser.util.JSONUtil;
import benny_driving_ser.util.SQL2ObjectListUtil;
import benny_driving_ser.util.SessionUtil;

public class OrderAction extends BaseAction {

	private OrderDao dao;
	private PrintWriter out;
	private int driId;
	private int userId;
	/**
	 * 司机历史订单数据
	 */
	private JSONObject orderJson;
	private JSONArray usedforms;
	private DriHistoryOrderBean driHB;
	private List<DriHistoryOrderBean> driHBs;
	private int startPage;

	/**
	 * 用户预约订单数据
	 */
	private JSONObject userJson;
	private JSONArray resforms;
	private UserOrderBean userOrder;
	private List<UserOrderBean> userOrders;
	/**
	 * 用户历史订单数据
	 */
	private UserHistoryOrderBean userHistoryOrder;
	private List<UserHistoryOrderBean> userHistoryOrders;
	private JSONObject userHistoryJson;
	
	/**
	 * 司机提现,充值
	 */
	private int cost;
	

	
//	/**
//	 * 加载司机历史订单
//	 * @return
//	 */
//	public String loadDriHistoryOrder() {
//		
//		response.setContentType("text/html");
//		response.setCharacterEncoding("utf-8");
//		
//		SessionAttributeDriId sad = SessionUtil.getInstance()
//				.verifySessionIdForDri(request);
//		try {
//			out = response.getWriter();
//			if(sad!=null){
//				HashMap<String,String> map = new HashMap<String,String>();
//				String sql = "select t.id,t.start_time,t.end_time,t.gk,t.gksj,u.xm,t.qd,"
//						+ "t.zd,t.sjqd,t.sjzd,t.total,t.pay,t.sjlc,t.jl,t.jg,t.ksje "
//						+ "from t_djpd t,t_user u where t.dri_id= " + sad.getValue() + " and t.state>5 and t.dri_id = u.id";
//				
//				map.put("adesPos","sjzd");//1
//				map.put("astartPos","sjqd");//2
//				map.put("cost", "total");//3
//				map.put("costWay","pay");//4
//				map.put("dedCost","total");//5
//				map.put("desPos","zd");//6
//				map.put("driName","xm");//7
//				map.put("startTime","start_time");//8
//				map.put("endTime","end_time");//9
//				map.put("exCost","jg");//0
//				map.put("exMil","jl");//1
//				map.put("formId","id");//2
//				map.put("mil","sjlc");//3
//				map.put("startsPos","qd");//4
//				map.put("userName","gk");//5
//				map.put("userTel","gksj");//6
//				
//				driHB = new DriHistoryOrderBean();
//				
//				driHBs = SQL2ObjectListUtil.sql2ObjectList(driHB, sql, map);
//				String str = JSONUtil.object2json("driHBs", driHBs);
//				System.out.println("str:"+str);
//				out.println(str);
//				out.flush();
//				out.close();
//				return "success";
//				
//			}else{
//				out.println(-1);
//				out.flush();
//				out.close();
//				
//			}
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			out.println("{'resforms':[]}");
//			out.flush();
//			out.close();
//			e.printStackTrace();
//		}
//		
//		
//		
//		return "success";
//	}
	
	
	

	/**  
	 * 加载司机历史订单
	 * 
	 * @return
	 */
	public String loadDriHistoryOrder() {

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");

		dao = new OrderDaoImpl();
		driHB = new DriHistoryOrderBean();
		driHBs = new ArrayList<DriHistoryOrderBean>();
		orderJson = new JSONObject();
		usedforms = new JSONArray();

		SessionAttributeDriId sad = SessionUtil.getInstance()
				.verifySessionIdForDri(request);

		try {
			out = response.getWriter();
			if (sad != null) {
				driId = Integer.parseInt(sad.getValue());
				driHBs = dao.loadDriHistoryOrder(driId,startPage);
				if (driHBs != null) {
					for (DriHistoryOrderBean driHB : driHBs) {
						JSONObject mjo = new JSONObject();
						mjo.put("formid", driHB.getFormId());
						mjo.put("starttime", driHB.getStartTime());
						mjo.put("endtime", driHB.getEndTime());
						mjo.put("username", driHB.getUserName());
						mjo.put("usertel", driHB.getUserTel());
						mjo.put("driname", driHB.getDriName());
						mjo.put("startpos", driHB.getStartsPos());
						mjo.put("despos", driHB.getDesPos());
						mjo.put("astartpos", driHB.getAstartPos());
						mjo.put("adespos", driHB.getAdesPos());
						mjo.put("cost", driHB.getCost());
						mjo.put("costway", driHB.getCostWay());
						mjo.put("mil", driHB.getMil());
						mjo.put("exmil", driHB.getExMil());
						mjo.put("excost", driHB.getExCost());
						mjo.put("dedcost", driHB.getDedCost());
						mjo.put("bonus",driHB.getBonus());
						mjo.put("bz", driHB.getBz());
						mjo.put("paycodecost", driHB.getPayCodeCost());
						
						usedforms.put(mjo);
					}
					orderJson.put("usedforms", usedforms);
					out.println(orderJson.toString());
					out.flush();
					out.close();
					return null;

				} else {
					out.print("{'usedforms':[]}");
				}
			}
			out.print(-1);
			out.flush();
			out.close();

		} catch (JSONException e) {
			out.print("{'usedforms':[]}");
			e.printStackTrace();
		} catch (IOException e) {
			out.print("{'usedforms':[]}");
			e.printStackTrace();
		} catch (NumberFormatException e) {
			out.print("{'usedforms':[]}");
			e.printStackTrace();
		}

		return "success";

	}
	
	
	/**
	 * 加载用户预约订单
	 * 
	 * @return
	 */
	public String loadUserOrder() {
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");

		dao = new OrderDaoImpl();
		userOrder = new UserOrderBean();
		userOrders = new ArrayList<UserOrderBean>();
		userJson = new JSONObject();
		resforms = new JSONArray();

		SessionAttributeUserId sad = SessionUtil.getInstance()
				.verifySessionIdForUser(request);

		try {
			if (sad != null) {
				if (userOrders != null) {
					userId = Integer.parseInt(sad.getValue());
					userOrders = dao.loadUserOrder(userId);
					out = response.getWriter();

					for (UserOrderBean userOrder : userOrders) {
						JSONObject mjo = new JSONObject();

						mjo.put("formid", userOrder.getFormId());
						mjo.put("time", userOrder.getTime());
						mjo.put("username", userOrder.getUserName());
						mjo.put("usertel", userOrder.getUserTel());
						mjo.put("dritel", userOrder.getDriTel());
						mjo.put("drinum", userOrder.getDriNum());
						mjo.put("startpos", userOrder.getStartPos());
						mjo.put("despos", userOrder.getDesPos());
						mjo.put("exmil", userOrder.getExmil());
						mjo.put("excost", userOrder.getExcost());
						mjo.put("formstate", userOrder.getFormState());
						mjo.put("driname", userOrder.getDriName());
						resforms.put(mjo);
					}
					userJson.put("resforms", resforms);
					out.println(userJson.toString());
					out.flush();
					out.close();
				} else {
					out.print("{'resforms':[]}");
				}
			}

			out.println(-1);
			out.flush();
			out.close();
		} catch (IOException e) {
			out.print("{'resforms':[]}");
			e.printStackTrace();
		} catch (NumberFormatException e) {
			out.print("{'resforms':[]}");
			e.printStackTrace();
		} catch (JSONException e) {
			out.print("{'resforms':[]}");
			e.printStackTrace();
		}

		return "success";

	}
	
	
	
//
//	/**
//	 * 加载用户预约订单
//	 * 
//	 * @return
//	 */
//	public String loadUserOrder() {
//		response.setContentType("text/html");
//		response.setCharacterEncoding("utf-8");
//
//		dao = new OrderDaoImpl();
//		userOrder = new UserOrderBean();
//		userOrders = new ArrayList<UserOrderBean>();
//		userJson = new JSONObject();
//		resforms = new JSONArray();
//
//		SessionAttributeUserId sad = SessionUtil.getInstance()
//				.verifySessionIdForUser(request);
//
//		try {
//			if (sad != null) {
//				if (userOrders != null) {
//					userId = Integer.parseInt(sad.getValue());//4605;//
//					userOrders = dao.loadUserOrder(userId);
//					out = response.getWriter();
//
//					for (UserOrderBean userOrder : userOrders) { 
//						JSONObject mjo = new JSONObject();
//
//						mjo.put("formid", userOrder.getFormId());
//						mjo.put("time", userOrder.getTime());
//						mjo.put("username", userOrder.getUserName());
//						mjo.put("usertel", userOrder.getUserTel());
//						mjo.put("dritel", userOrder.getDriTel());
//						mjo.put("drinum", userOrder.getDriNum());
//						mjo.put("startpos", userOrder.getStartPos());
//						mjo.put("despos", userOrder.getDesPos());
//						mjo.put("exmil", userOrder.getExmil());
//						mjo.put("excost", userOrder.getExcost());
//						mjo.put("formstate", userOrder.getFormState());
//						mjo.put("driname", userOrder.getDriName());
//						mjo.put("cardstate",userOrder.getCardState());
//						mjo.put("cardvalue", userOrder.getCardValue());
//						resforms.put(mjo);
//					}
//					userJson.put("resforms", resforms);
//					out.println(userJson.toString());
//					out.flush();
//					out.close();
//				} else {
//					out.print("{'resforms':[]}");
//				}
//			}
//
//			out.println(-1);
//			out.flush();
//			out.close();
//		} catch (IOException e) {
//			out.print("{'resforms':[]}");
//			e.printStackTrace();
//		} catch (NumberFormatException e) {
//			out.print("{'resforms':[]}");
//			e.printStackTrace();
//		} catch (JSONException e) {
//			out.print("{'resforms':[]}");
//			e.printStackTrace();
//		}
//
//		return "success";
//
//	}

	/**
	 * 加载用户历史订单
	 * 
	 * @return
	 */
	public String loadUserHistoryOrder() {
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");

		dao = new OrderDaoImpl();
		userHistoryOrder = new UserHistoryOrderBean();
		userHistoryOrders = new ArrayList<UserHistoryOrderBean>();
		userHistoryJson = new JSONObject();
		usedforms = new JSONArray();

		SessionAttributeUserId sad = SessionUtil.getInstance()
				.verifySessionIdForUser(request);

		try {
			if (sad != null) {
				userId = Integer.parseInt(sad.getValue());
				userHistoryOrders = dao.loadUserHistoryOrder(userId);
				out = response.getWriter();

				if (userHistoryOrders != null) {
					for (UserHistoryOrderBean userHistoryOrder : userHistoryOrders) {
						JSONObject mjo = new JSONObject();
						mjo.put("formid", userHistoryOrder.getFormId());
						mjo.put("username", userHistoryOrder.getUserName());
						mjo.put("usertel", userHistoryOrder.getUserTel());
						mjo.put("waittime", userHistoryOrder.getWaitTime());
						mjo.put("driname", userHistoryOrder.getDriName());
						mjo.put("dritel", userHistoryOrder.getDriTel());
						mjo.put("drinum", userHistoryOrder.getDriNum());
						mjo.put("startpos", userHistoryOrder.getStartPos());
						mjo.put("despos", userHistoryOrder.getDesPos());
						mjo.put("starttime", userHistoryOrder.getStartTime());
						mjo.put("endtime", userHistoryOrder.getEndTime());
						mjo.put("cost", userHistoryOrder.getCost());
						mjo.put("costway", userHistoryOrder.getCostWay());
						mjo.put("mil", userHistoryOrder.getMil());
						mjo.put("iscomment", userHistoryOrder.getIsComment());

						usedforms.put(mjo);
						
					}
					userHistoryJson.put("usedforms", usedforms);
					out.println(userHistoryJson.toString());
					out.flush();
					out.close();
					return null;

				} else {
					out.print("{'resforms':[]}");
				}

			}
			out.println(-1);
			out.flush();
			out.close();

		} catch (JSONException e) {
			out.print("{'resforms':[]}");
			e.printStackTrace();
		} catch (IOException e) {
			out.print("{'resforms':[]}");
			e.printStackTrace();
		} catch (NumberFormatException e) {
			out.print("{'resforms':[]}");
			e.printStackTrace();
		}

		return "success";
	}

	/**
	 * 生成电话订单
	 * 
	 * @return
	 */

	// public String createTelOrder(){
	// int i = 0;
	// try {
	// response.setContentType("text/html");
	// response.setCharacterEncoding("utf-8");
	// out = response.getWriter();
	// dao = new OrderDaoImpl();
	// i = dao.createTelOrder(userId, driId);
	// out.println(i);
	// out.flush();
	// out.close();
	// return "success";
	// } catch (IOException e) {
	// e.printStackTrace();
	// }
	// out.println(i);
	// out.flush();
	// out.close();
	// return "fail";
	// }

	
	/**
	 * 33号接口(服务器端已经写好,还没有关联网银)
	 * 司机提现
	 * @return
	 */
	public String getDeposit(){
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		int backFlag = 1;
		
		SessionAttributeDriId sad = SessionUtil.getInstance()
				.verifySessionIdForDri(request);
		try{
			out = response.getWriter();
			if(sad!=null){
				
				driId = Integer.parseInt(sad.getValue());
				dao = new OrderDaoImpl();
				backFlag = dao.getDeposit(driId, cost);
			}
			
		}catch(IOException e){
			e.printStackTrace();
		}catch(NumberFormatException e){
			e.printStackTrace();
		}
		out.print(backFlag);
		out.flush();
		out.close();
		return backFlag==0?"success":"fail";
	}
	
	
	/**
	 * 司机未完成订单
	 * 0没有未完成订单,1有未完成订单,2IO异常,3数组格式化异常,-1登陆超时
	 * @return
	 */
	public String driUnfinishedOrder(){
		int i = 1;
		
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		
		SessionAttributeDriId sad = SessionUtil.getInstance()
				.verifySessionIdForDri(request);
		try{
			driId = Integer.parseInt(sad.getValue());
			dao = new OrderDaoImpl();
			out = response.getWriter();
			if(sad!=null){
				i= dao.driUnfinishedOrder(driId);
				if(i==0){
					//没有订单
					out.println(i);
					out.flush();
					out.close();
					return "success";
				}
				//有订单
				out.println(1);
				out.flush();
				out.close();
				return "fail";
			}else{
				out.println(-1);
				out.flush();
				out.close();
			}
			
			
		}catch(IOException e){
			out.println(2);
			out.flush();
			out.close();
			e.printStackTrace();
		}catch(NumberFormatException e){
			out.println(3);
			out.flush();
			out.close();
			e.printStackTrace();
		}
		return "fail";
	}
	
	
	
	
	
//	/**
//	 * 34号接口(服务器断已经写好,还没有关联网银)
//	 * 司机充值
//	 * @return
//	 */
//	public String driPay(){
//		response.setContentType("text/html");
//		response.setCharacterEncoding("utf-8");
//		int backFlag = 1;
//		SessionAttributeDriId sad = SessionUtil.getInstance()
//				.verifySessionIdForDri(request);
//		try{
//			dao = new OrderDaoImpl();
//			out = response.getWriter();
//			driId = Integer.parseInt(sad.getValue());
//			if(sad!=null){
//				backFlag = dao.driPay(driId, cost);
//			}
//			
//		}catch(IOException e){
//			e.printStackTrace();
//		}catch(NumberFormatException e){
//			e.printStackTrace();
//		}
//		out.println(backFlag);
//		out.flush();
//		out.close();
//		return backFlag==0?"success":"fail";
//	}
	
	
	
	
	public OrderDao getDao() {
		return dao;
	}

	public void setDao(OrderDao dao) {
		this.dao = dao;
	}

	public DriHistoryOrderBean getDriHB() {
		return driHB;
	}

	public void setDriHB(DriHistoryOrderBean driHB) {
		this.driHB = driHB;
	}

	public int getDriId() {
		return driId;
	}

	public void setDriId(int driId) {
		this.driId = driId;
	}

	public JSONArray getUsedforms() {
		return usedforms;
	}

	public void setUsedforms(JSONArray usedforms) {
		this.usedforms = usedforms;
	}

	public PrintWriter getOut() {
		return out;
	}

	public void setOut(PrintWriter out) {
		this.out = out;
	}

	public List<DriHistoryOrderBean> getDriHBs() {
		return driHBs;
	}

//	public List<?> getDriHBs() {
//		return driHBs;
//	}
	
	public void setDriHBs(List<DriHistoryOrderBean> driHBs) {
		this.driHBs = driHBs;
	}

	public JSONObject getOrderJson() {
		return orderJson;
	}

	public void setOrderJson(JSONObject orderJson) {
		this.orderJson = orderJson;
	}

	public int getUserId() {
		return userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}

	public UserOrderBean getUserOrder() {
		return userOrder;
	}

	public void setUserOrder(UserOrderBean userOrder) {
		this.userOrder = userOrder;
	}

	public List<UserOrderBean> getUserOrders() {
		return userOrders;
	}

	public void setUserOrders(List<UserOrderBean> userOrders) {
		this.userOrders = userOrders;
	}

	public UserHistoryOrderBean getUserHistoryOrder() {
		return userHistoryOrder;
	}

	public void setUserHistoryOrder(UserHistoryOrderBean userHistoryOrder) {
		this.userHistoryOrder = userHistoryOrder;
	}

	public List<UserHistoryOrderBean> getUserHistoryOrders() {
		return userHistoryOrders;
	}

	public void setUserHistoryOrders(
			List<UserHistoryOrderBean> userHistoryOrders) {
		this.userHistoryOrders = userHistoryOrders;
	}

	public JSONObject getUserHistoryJson() {
		return userHistoryJson;
	}

	public void setUserHistoryJson(JSONObject userHistoryJson) {
		this.userHistoryJson = userHistoryJson;
	}

	public JSONObject getUserJson() {
		return userJson;
	}

	public void setUserJson(JSONObject userJson) {
		this.userJson = userJson;
	}

	public JSONArray getResforms() {
		return resforms;
	}

	public void setResforms(JSONArray resforms) {
		this.resforms = resforms;
	}

	public int getCost() {
		return cost;
	}

	public void setCost(int cost) {
		this.cost = cost;
	}

	public int getStartPage() {
		return startPage;
	}

	public void setStartPage(int startPage) {
		this.startPage = startPage;
	}

	
}
